package cn.lutu.adapter.controller;

import cn.lutu.domain.service.HotelService;
import cn.lutu.protocol.dto.HotelAddDTO;
import cn.lutu.protocol.dto.HotelDTO;
import cn.lutu.protocol.vo.ArticleOthersListVO;
import cn.lutu.protocol.vo.HotelVO;
import cn.lutu.protocol.vo.RoomVO;
import cn.lutu.util.pojo.vo.PageData;
import cn.lutu.util.response.ResultVO;
import cn.lutu.util.response.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "酒店产品模块")
@RestController
@RequestMapping("/v1/hotels")
public class HotelController {
    @Autowired
    HotelService hotelService;

    @ApiOperation(value = "添加酒店产品功能")
    @PostMapping("add")
    @PreAuthorize("hasAnyAuthority('/product/addNew')")
    public ResultVO insert(@RequestBody HotelAddDTO hotelDTO) {
    hotelService.insertHotel(hotelDTO);
    return ResultVO.ok();
    }


    @ApiOperation(value = "修改酒店产品功能")
    @PostMapping("update")
    @PreAuthorize("hasAnyAuthority('/product/update')")
    public ResultVO updateById(@RequestBody HotelDTO hotelDTO){
       hotelService.updateById(hotelDTO);
        return ResultVO.ok();
    }

    @ApiOperation(value = "删除酒店产品功能")
    @PostMapping("/{id}/delete")
    @PreAuthorize("hasAnyAuthority('/product/delete')")
    public ResultVO deleteHotelById( @PathVariable Long id){
        hotelService.deleteHotelById(id);
        return ResultVO.ok();
    }
    @ApiOperation(value = "删除房间产品功能")
    @PostMapping("/{id}/deleteRoom")
    @PreAuthorize("hasAnyAuthority('/product/delete')")
    public ResultVO deleteById( @PathVariable Long id){
//        int userId = (int) request.getAttribute("userId");
//        if(product.getUserId() != userId){
//            // 用户不具有删除此产品的权限
//            return new  ResultVO(StatusCode.FORBIDDEN_ERROR);
//        }
            hotelService.deleteById(id);
            return ResultVO.ok();
    }

    @ApiOperation(value = "查询酒店产品功能")
    @GetMapping("/{id}/select")
    public ResultVO selectHotel(@PathVariable Long id){
        HotelVO hotelVO= hotelService.selectHotel(id);
        if (hotelVO==null){
            return new ResultVO(StatusCode.OPERATION_FAILED);
        }
        return ResultVO.ok(hotelVO);
    }

    @ApiOperation(value = "查询房间产品功能")
    @GetMapping("/{id}/selectRoom1")
    public ResultVO selectRoom1(@PathVariable Long id){
        HotelVO hotelVO = hotelService.selectRoom(id);
        if (hotelVO==null){
            return new ResultVO(StatusCode.OPERATION_FAILED);
        }
            return ResultVO.ok(hotelVO);
    }

    @ApiOperation(value = "酒店列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    @GetMapping("/list")
    public ResultVO select(Integer page, String queryType){
        log.debug("开始处理【酒店列表】请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<HotelVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = hotelService.selectHotelLIst(1, Integer.MAX_VALUE);
        } else {
            pageData = hotelService.selectHotelLIst(pageNum);
        }
        log.info("查询结束，查询结果为{}",pageData);
        return ResultVO.ok(pageData);
    }


    @ApiOperation(value = "查询首页所有酒店")
    @GetMapping("")
    public ResultVO selectAllIndex(){
        log.debug("根据ID查询酒店数据，参数{}");
        List<HotelVO> list = hotelService.selectAllHotel();
        return ResultVO.ok(list);
    }

    @ApiOperation(value = "查询所有酒店")
    @GetMapping("hotelsList")
    public ResultVO selectAll(){
        log.debug("根据ID查询酒店数据，参数{}");
        List<HotelVO> list = hotelService.selectAllHotels();
        return ResultVO.ok(list);
    }

    @ApiOperation(value = "查询酒店详情")
    @GetMapping("/{id}/list")
    @PreAuthorize("hasAnyAuthority('/product/detials')")
    public ResultVO selectById(@PathVariable Long id){
        log.debug("根据ID查询酒店详情数据，参数{}",id);
        HotelVO hotelVO = hotelService.selectHotelLIstById(id);
        return ResultVO.ok(hotelVO);
    }

    @ApiOperation(value = "查询房间类型")
    @GetMapping("/{id}/selectRoom")
    @PreAuthorize("hasAnyAuthority('/product/detials')")
    public ResultVO selectRoom(@PathVariable Long id){
        log.debug("根据ID查询房间详情数据，参数{}",id);
        List<RoomVO> list = hotelService.selectRoomById(id);
        return ResultVO.ok(list);
    }








}

